package com.example.java8.feature_03;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TestLocalDateTime {

    public static void main(String[] args){

        // LocalDateTime相当于 LocalDate + LocalTime
        LocalDateTime nowDateTime = LocalDateTime.now();
        System.out.println("当前日期时间:" + nowDateTime);

        // LocalDateTime常用方法(get, plus, minus, with)系列方法与 LocalDate&LocalTime大同小异
        // ...

        // 获取指定日期时间对象-LocalDateTime提供一系列的of静态方法来获取指定日期时间对象
        LocalDateTime dateTime = LocalDateTime.of(2016, 10, 7, 16, 24);
        System.out.println("指定日期时间:" + dateTime);

        // 计算时间差(nowDateTime - dateTime)
        Duration duration = Duration.between(dateTime, nowDateTime);
        System.out.println("相差天数:" + duration.toDays());
        System.out.println("相差小时数:" + duration.toHours());
        System.out.println("相差分钟数:" + duration.toMinutes());
        System.out.println("相差毫秒数:" + duration.toMillis());
        System.out.println("相差纳秒数:" + duration.toNanos());

        // 格式化,Java8新增了线程安全的DateTimeFormatter来处理日期时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter.format(dateTime));

    }

}
